/**
 * @Description: 两种生成水印的方式，都一样
 * @Author: shanansun
 * @Date:  20200426
 */
import { connect } from 'dva';
import { withRouter } from 'umi';
import React from 'react';
import Big from 'big.js';

import WaterMarkComponentAp from './waterMarkComponentAp';

@withRouter
class WaterMarkAp extends React.Component {
  static defaultProps = {
    watermarkConfig: [],
  };
  countdown;
  countdownfn = () => {
    this.countdown = setTimeout(
      () => {
        this.getConfig();
      },
      new Big(2)
        .times(60)
        .times(1000)
        .toString()
    );
  };
  clearcountdownfn = () => {
    clearTimeout(this.countdown);
    this.countdownfn();
  };
  getConfig() {
    this.props.queryWaterMark().then(e => {
      // if (
      //   this.props.watermarkConfig.find(item => item.url === this.props.history.location.pathname)
      // ) {
      // if (this.props.watermarkConfig.isOn) {
      // } else {
      // this.childFuntion();
      this.clearcountdownfn();
      // }
      // }
    });
  }
  componentDidMount() {
    this.props.getUserInfo();
    this.props.history.listen(e => this.getConfig());
  }
  render() {
    // return this.props.loadingqueryWaterMark ? null : this.props.watermarkConfig.isOn ? (
    //   this.props.children
    // ) : (
    //   <WaterMarkComponent watermarkConfig={this.props.watermarkConfig}>
    //     {this.props.children}
    //   </WaterMarkComponent>
    // );
    return (this.props.userInfo.userCode &&
      this.props.watermarkConfig.find(item => item.url == '*')) ||
      this.props.watermarkConfig.find(item => item.url == this.props.history.location.pathname) ? (
      <WaterMarkComponentAp
        watermarkConfig={
          this.props.watermarkConfig.find(item => item.url == '*') ||
          this.props.watermarkConfig.find(item => item.url == this.props.history.location.pathname)
        }
        getFuntion={e => (this.childFuntion = e)}
        userInfo={this.props.userInfo.userCode}
      >
        {this.props.children}
      </WaterMarkComponentAp>
    ) : (
      this.props.children
    );
  }
}

export default connect(
  state => ({
    watermarkConfig: state.watermark.applicationPoList,
    userInfo: state.user.currentUser,
    loadingqueryWaterMark: state.loading.effects['watermark/queryWaterMarkApplicationPoList'],
  }),
  dispatch => ({
    getUserInfo: e => dispatch({ type: 'login/queryUserDetail', payload: e }),
    queryWaterMark: e =>
      dispatch({
        type: 'watermark/queryWaterMarkApplicationPoList',
        payload: { resCode: 'AHMGSJ' },
      }),
  })
)(WaterMarkAp);
